package com.scy.boot04test01.controller;

import com.scy.boot04test01.entity.Emp;
import com.scy.boot04test01.mapper.EmpMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class EmpController {
    @Autowired
    EmpMapper empMapper;

    @RequestMapping("/insertEmp")
    public String insert(Emp emp){
        System.out.println("emp = " + emp);
        empMapper.insert(emp);
        return "添加成功<a href='/'>返回首页</a>";
    }

    @RequestMapping("/selectEmp")
    public String select(){
        List<Emp> list = empMapper.select();
        //把list集合中的数据装金表格标签
        String html = "<table border='1'>";
        html+="<caption>员工列表</caption>";
        html+="<tr><th>id</th><th>姓名</th><th>工资</th><th>工作</th><th>操作</th></tr>";
        for (Emp e :
                list) {
            html+="<tr>";
            html+="<td>"+e.getId()+"</td>";
            html+="<td>"+e.getName()+"</td>";
            html+="<td>"+e.getSalary()+"</td>";
            html+="<td>"+e.getJob()+"</td>";
            //http://localhost:8080/delete?id=xxx
            html+="<td><a href='/deleteEmp?id="+e.getId()+"'>删除</a></td>";
            html+="</tr>";
        }
        html+="</table>";
        return html+"<br/>"+"<a href='/'>返回首页</a>";

    }
    @RequestMapping("/deleteEmp")
    public String delete(int id){
        System.out.println("id = " + id);
        empMapper.delete(id);
        return "删除成功！！！<a href='/'>返回首页</a>";
    }

    @RequestMapping("/updateEmp")
    public String update(Emp emp){
        empMapper.update(emp);
        return "修改成功！！！<a href='/'>返回首页</a>";
    }
}
